Firefox 1.5, Internet Explorer 6.0 SP2. Opera 8.5のWebブラウザのXML DOMの動作を調べてたところ、かなり深刻な非互換性があることが分かりました。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html LANG="ja-JP">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
<META http-equiv="Content-Script-Type" content="text/javascript">
<title>テストページ</title>
<script type="text/javascript"><!--
var text;
function dumpSub(node,spaces)
{
text = text + spaces + "node start" + "\n";
text = text + spaces + "nodeType: " + node.nodeType + "\n";
text = text + spaces + "nodeName: " + node.nodeName + "\n";
text = text + spaces + "namespaceURI: " + node.namespaceURI + "\n";
text = text + spaces + "nodeValue: " + node.nodeValue + "\n";
text = text + spaces + "localName: " + node.localName + "\n";
text = text + spaces + "prefix: " + node.prefix + "\n";
if( node.nodeType == 1 )
{
text = text + spaces + "attributes:\n";
for( var i=0; i<node.attributes.length; i++ )
{
var attr = node.attributes[i];
var s = spaces + " ";
text = text + s + "nodeType: " + attr.nodeType + "\n";
text = text + s + "nodeName: " + attr.nodeName + "\n";
text = text + s + "namespaceURI: " + attr.namespaceURI + "\n";
text = text + s + "localName: " + attr.localName + "\n";
text = text + s + "prefix: " + attr.prefix + "\n";
text = text + s + "name: " + attr.name + "\n";
text = text + s + "value: " + attr.value + "\n";
}
}
for( var i=0; i<node.childNodes.length; i++ )
{
text = text + spaces + "childNodes:\n";
dumpSub(node.childNodes[i],spaces + " ");
}
text = text + spaces + "node end" + "\n";
}
function dump(name)
{
try
{
httpobj = createHttpRequest();
httpobj.open( "GET", name, false );
httpobj.send(null);
text = "";
dumpSub(httpobj.responseXML,"");
document.getElementById('text').value = text;
}
catch( ex )
{
document.getElementById('text').value = "ファイルが読み込めませんでした。";
}
}
function createHttpRequest()
{
//Win IE用
if(window.ActiveXObject){
try {
//MSXML2以降用
return new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
try {
//旧MSXML用
return new ActiveXObject("Microsoft.XMLHTTP")
} catch (e2) {
return null
}
}
} else if(window.XMLHttpRequest){
//Win IE以外のXMLHttpRequestオブジェクト実装ブラウザ用
return new XMLHttpRequest()
} else {
return null
}
}
// --></script>
</head>
<body>
<p>
ファイル名:
<input id="fileName" type="text">
<input type="button" onclick="dump(document.getElementById('fileName').value);"
value="読み込む" ></input>
</p>
<textarea id="text" rows="10" cols="60" readOnly></textarea>
</body>
</html>
node start
nodeType: 9
nodeName: #document
namespaceURI: null
nodeValue: null
localName: null
prefix: null
childNodes:
node start
nodeType: 1
nodeName: ns:sample
namespaceURI: http://example.com/
nodeValue: null
localName: sample
prefix: ns
attributes:
nodeType: 2
nodeName: xmlns:ns
namespaceURI: http://www.w3.org/2000/xmlns/
localName: xmlns:ns
prefix: xmlns
name: xmlns:ns
value: http://example.com/
nodeType: 2
nodeName: ns:attr ←ns:はXML文書中にない表記
↓指定されていない名前空間に属していることにされている(※)
namespaceURI: http://example.com/
localName: ns:attr ←ns:はXML文書中にない表記
prefix: ns ←ns:はXML文書中にない表記
name: ns:attr ←ns:はXML文書中にない表記
value: attval
childNodes:
node start
nodeType: 3
nodeName: #text
namespaceURI: null
nodeValue: Hello!
localName: null
prefix: null
node end
node end
node end
※ 名前空間接頭辞のない属性は、その属性を含む要素に固有の名前空間に属するが、その名前空間は名前によって識別される意味がないので、名前が存在しない。つまり、名前空間URIがあるのはおかしい。ちなみに、名前空間URIのある属性は、大域的な名前空間に属するが、これは明示的に接頭辞の付いた属性だけが該当するものであり、上記の動作はその意味でも正しくない……、はず。